const {join} = require('path');
const {AnalyController,AnalyPojo,AnalyTType} = require('../ControllerAnaly');
const {tmpClassPathName} = require('../util');
const fs = require('fs');
const tmppath = join(__dirname,'./dist');
const controller = {
  isController:/^@RestController$/,
  urlAnno:/^@RequestMapping\(/,
  path:/com.(\w+)/,
  action(anno,topUrl){//返回的结构是{value:"/logout",method:"RequestMethod.GET"}
    return {
      url:`${topUrl}${anno.value}`,
      action:anno.method=='RequestMethod.POST'?'post':'get'
    };
  }
}

const list = require(join(__dirname,'./right-result',tmpClassPathName));

function fetchPojo(id){
  console.log('递归依赖：',id);
  if(typeof id=='string'){
    const d = list.find(m=>`${m.package}.${m.class}`==id);
    if(d.T.length==0){//存在泛型,不保存
      return AnalyPojo(d,fetchPojo);
    }else{
      return d;
    }
  }else{
    //泛型处理
    return AnalyTType(id,fetchPojo);
  }
}

/********解析ADto内容,最简单的参数 ***/
if(false){
  const c = list.find(m=>m.class=='ADto');

  const a = AnalyPojo(c,()=>{
    //没有依赖项
  })
  
  fs.writeFile(join(tmppath,'/api/a.json'),JSON.stringify(a,null,2),(err)=>{
    if(!err){
      console.log('解析ADto类成功')
    }
  })
}

/**解析DDto，主要是继承功能测试 */
if(false){
  const c = list.find(m=>m.class=='DDto');
  const a = AnalyPojo(c,fetchPojo)
  fs.writeFile(join(tmppath,'/api/d.json'),JSON.stringify(a,null,2),(err)=>{
    if(!err){
      console.log('解析DDto类成功')
    }
  })
}
/**解析EDto泛型 */
if(false){
  const c = list.find(m=>m.class=='EDto');
  const a = AnalyPojo(c,fetchPojo)
  fs.writeFile(join(tmppath,'/api/e.json'),JSON.stringify(a,null,2),(err)=>{
    if(!err){
      console.log('解析EDto类成功')
    }
  })

}
/**解析FDto多层泛型+数组 */
if(false){
  const c = list.find(m=>m.class=='FDto');
  const a = AnalyPojo(c,fetchPojo)
  fs.writeFile(join(tmppath,'/api/f.json'),JSON.stringify(a,null,2),(err)=>{
    if(!err){
      console.log('解析FDto类成功')
    }
  })
}
if(true){
  // const c = list.find(m=>m.class=='RespBaseDto');
  // const a = AnalyPojo(c,fetchPojo)
  // fs.writeFile(join(tmppath,'/api/resp.json'),JSON.stringify(a,null,2),(err)=>{
  //   if(!err){
  //     console.log('解析RespBaseDto类成功')
  //   }
  // })
}
/**解析Controller */
if(true){
  const c = list.find(m=>m.class=='TestController');
  const a = AnalyController(c,fetchPojo)
  fs.writeFile(join(tmppath,'/api/testController.json'),JSON.stringify(a,null,2),(err)=>{
    if(!err){
      console.log('解析TestController类成功')
    }
  })
}